package com.calsoft.Hops_Mobile_Tablet.card_ui_fragment;
/*
 * User: Minh Diep
 * Date: 6/28/13
 * Time: 11:31 AM
 */

import android.content.Intent;
import android.graphics.Typeface;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import com.calsoft.Hops_Mobile_Tablet.CardUIDisplayActivity;
import com.calsoft.Hops_Mobile_Tablet.R;
import com.calsoft.Hops_Mobile_Tablet.base.App;
import com.calsoft.Hops_Mobile_Tablet.dummy.Flight;
import com.calsoft.Hops_Mobile_Tablet.dummy.StaticData;

public class FlightCardUIFragment extends Fragment {

    public FlightCardUIFragment() {
        // Empty constructor required for fragment subclasses
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.fragment_card_ui_flight, container, false);
        getActivity().setTitle(App.APP_TITLE);

        StaticData staticData = new StaticData();
        String itemId = getArguments().getString(CardUIDisplayActivity.CARD_UI_ITEM_ID, "");
        final Flight flight = staticData.flightHashMap.get(itemId);

        TextView titleTv = (TextView) rootView.findViewById(R.id.card_ui_item_title);
        TextView nameTv = (TextView) rootView.findViewById(R.id.card_ui_item_name_tv);
        ImageView mainIv = (ImageView) rootView.findViewById(R.id.card_ui_item_main_iv);
        TextView flightNumberTv = (TextView) rootView.findViewById(R.id.flight_number_tv);
        TextView departTv = (TextView) rootView.findViewById(R.id.depart_tv);
        TextView departInfoTv = (TextView) rootView.findViewById(R.id.depart_info_tv);
        TextView arriveTv = (TextView) rootView.findViewById(R.id.arrive_tv);
        TextView arriveInfoTv = (TextView) rootView.findViewById(R.id.arrive_info_tv);

        Typeface tf = Typeface.createFromAsset(getActivity().getApplicationContext().getAssets(),
                "fonts/Roboto-Light.ttf");
        titleTv.setTypeface(tf);
        nameTv.setTypeface(tf);

        mainIv.setImageResource(R.drawable.card_flight);
        nameTv.setText(flight.getFlightName());
        flightNumberTv.setText(flight.getFlightNumber());
        departTv.setText(flight.getDepartureName().toUpperCase());
        departInfoTv.setText(flight.getDepartureInfo());
        arriveTv.setText(flight.getArrivalName().toUpperCase());
        arriveInfoTv.setText(flight.getArrivalInfo());


        LinearLayout flightStatusBtn = (LinearLayout) rootView.findViewById(R.id.flight_status_btn);
        LinearLayout flightCallBtn = (LinearLayout) rootView.findViewById(R.id.flight_call_btn);
        flightStatusBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                // TODO:HACK DATA
                String date = "";
                if (flight.getFlightNumber().equals("JQ016")){
                    date = "07";
                } else if (flight.getFlightNumber().equals("JQ929")) {
                    date = "11";
                } else if (flight.getFlightNumber().equals("JQ019")) {
                    date = "13";
                }

                String flightNumber = flight.getFlightNumber().substring(2);
                String url = "http://mobile.flightstats.com/go/Mobile/flightStatusByFlight.do?airline=%28JQ%29+Jetstar+Airways&flightNumber=" + flightNumber + "&departureDate=2013-07-" + date;
                Intent intent = new Intent(android.content.Intent.ACTION_VIEW,  Uri.parse(url));
                startActivity(intent);
            }
        });

        flightCallBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(Intent.ACTION_DIAL);
                intent.setData(Uri.parse("tel:1 (866) 397-8170"));
                startActivity(intent);
            }
        });

        return rootView;
    }
}
